package cn.nju.zyy.cooperation;

import java.util.concurrent.TimeUnit;

class Car {
	private boolean waxOn = false;
	
	public synchronized void waxed() {
		waxOn = true;	// Ready to buff
		notifyAll();
	}
	
	public synchronized void buffed() {
		waxOn = false;	// Ready for another coat of wax
		notifyAll();
	}
	
	public synchronized void waitForWaxing() throws InterruptedException {
		while (waxOn == false) {
			wait();
		}
	}
	
	public synchronized void waitForBuffing() throws InterruptedException {
		while (waxOn == true) {
			wait();
		}
	}
}

class WaxOn implements Runnable {

	private Car car;
	
	public WaxOn(Car car) {
		this.car = car;
	}
	
	@Override
	public void run() {
		try {
			while (!Thread.interrupted()) {
				System.out.println("Wax On!");
				TimeUnit.MILLISECONDS.sleep(200);
				car.waxed();
				car.waitForBuffing();
			}
		} catch (Exception e) {
			System.out.println("Exiting via interrupt");
		}
		System.out.println("Ending Wax On task");
	}
	
}

public class WaxOMatic {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

	}

}
